package com.itheima.design.mode.builder;

/**
 * 建造者模式
 */
public class UserBuilder {

    private String name;
    private String password;
    private String nickName;
    private int age;

    public UserBuilder() {
    }

   public UserBuilder name(String name){
        this.name = name;
        return this;
   }

   public UserBuilder password(String password){
        this.password = password;
        return this;
   }

   public UserBuilder nickName(String nickName) {
        this.nickName = nickName;
        return this;
   }

    // build() 方法负责将 UserBuilder 中设置好的属性“复制”到 User 中。
    // 当然，可以在 “复制” 之前做点检验
    public User build() {
        if (name == null || password == null) {
            throw new RuntimeException("用户名和密码必填");
        }
        if (age <= 0 || age >= 150) {
            throw new RuntimeException("年龄不合法");
        }
        // 还可以做赋予”默认值“的功能
        if (nickName == null) {
            nickName = name;
        }
        return new User(name, password, nickName, age);
    }

   public UserBuilder age(int age){
        this.age=age;
        return this;
   }
}
